package com.console.front.service.impl;

import java.util.List;

import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.console.front.dao.UserDao;
import com.console.front.model.MovsModel;
import com.console.front.model.UserModel;
import com.console.front.service.UserService;
import com.console.front.support.Constant;
import com.console.front.support.HttpUtils;

@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;

	@Override
	public UserModel get(Long id) {
		return userDao.get(id);
	}

	@Override
	public void save(UserModel user) {
		userDao.save(user);		
	}

	@Override
	public UserModel login(String username, String password) {
		return userDao.login(username,password);
	}

	@Override
	public void initMovs(String rootUrl) {
		Document rootDocument = HttpUtils.getPageContent(rootUrl);
		if(rootDocument == null)
			return;
		List<MovsModel> models = HttpUtils.paseDocument(rootDocument);
		for(MovsModel model : models){
			userDao.initMovs(model);
		}
		
	}
	
	@Override
	public List<MovsModel> findMovs(int pageNum, int pageSize) {
		return userDao.findMovs(pageNum,pageSize);
	}

	@Override
	public String findMovsMp4(String movsPath) {
		return userDao.findMovsMp4(movsPath);
	}

	@Override
	public void updateNewData() {
		Document rootDocument = HttpUtils.getPageContent(Constant.HOST);
		if(rootDocument == null)
			return;
		List<MovsModel> models = HttpUtils.paseDocument(rootDocument);
		for(MovsModel model : models){
			userDao.updateNewData(model);
		}
	}

	@Override
	public UserModel recharge(String days,String uid) {
		return userDao.recharge(days,uid);
	}

	@Override
	public UserModel reg(String username, String password) {
		return userDao.reg(username,password);
	}


}
